package sw6.digipecs.widget;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;

public class AnimationLayout extends FrameLayout {
	
	//TODO Draglayout should probably also be using this
	
	//boolean mAnimating = false;
	ArrayList<Animation> mAnimations = new ArrayList<Animation>();

	public AnimationLayout(Context context) {
		super(context);
	}
	
	public AnimationLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public AnimationLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public void startAnimation(final View view, final Animation animation, final AnimationListener listener) {
		if (mAnimations.isEmpty()) {
			Log.i("sw6", "Animation list is empty, cleaing all children");
			removeViews(1, getChildCount() - 1);
		}
		
		mAnimations.add(animation);
		
		AnimationListener al = new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				if (listener != null) listener.onAnimationStart(animation);
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
				if (listener != null) listener.onAnimationRepeat(animation);
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				if (listener != null) listener.onAnimationEnd(animation);
				view.setVisibility(INVISIBLE);
				mAnimations.remove(animation);
			}
		};
		animation.setAnimationListener(al);
		
		addView(view);
		view.startAnimation(animation);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (!mAnimations.isEmpty()) return true;
		else return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!mAnimations.isEmpty()) return true;
		else return super.onTouchEvent(event);
	}

}
